<?php
class fs_local extends fs_base {
	function fs_load () {
		global $wapi;
		$this->host_name = $wapi->host_name;
	}
    function browse($newpath) {
    	$mpath = $newpath;
		$info = new SplFileInfo($newpath);
		if (!$info->isDir()) {
			return false;
		}
		$newpath = $info->getRealPath();
		$this->path = $mpath;
		$this->real_path = $newpath;
		// permissions check here!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

		$iterator = new DirectoryIterator($newpath);
		//$iterator = new RecursiveIteratorIterator($dir_iterator, RecursiveIteratorIterator::SELF_FIRST);
		// could use CHILD_FIRST if you so wish
		$size = 0;
		$res = array();
		foreach ($iterator as $file) {
			if ($file->getBasename() != '.' && $file->getBasename() != '..') {
				$f = new fs_file();
				$f->name = $file->getBasename();
				$f->path = $file->getPath();
				$f->realPath = $file->getRealPath();
				$f->isFile = $file->isFile();
				$f->isDir = !$f->isFile;
				$f->size = $file->getSize();
				$f->modified = $file->getMTime();

				$f->isReadable = $file->isReadable();
				$f->isWritable = $file->isWritable();
				$f->isExecutable = $file->isExecutable();

				$res[] = $f;
			} else {
				$stop = '';
			}
		}
		echo "Browsing: " . $newpath;
		$this->files = $res;
		return true;
    }
    function dir ($folder) {
        $folder = $this->clean('\\' . $folder, '\\',false);
        if ($folder != '\\') {
            $folder = rtrim($folder,'\\');
        }
		$dir_iterator = new RecursiveDirectoryIterator($folder);
		$iterator = new RecursiveIteratorIterator($dir_iterator, RecursiveIteratorIterator::SELF_FIRST);
		// could use CHILD_FIRST if you so wish
		$size = 0;
		$results = array();
		foreach ($iterator as $file) {
			$results[] = $file;
		    if ($file->isFile()) {
		        echo $file->getPathname() . ": " . $file->getSize() . " B; modified " . date("Y-m-d", $file->getMTime()) . "<br/>\n";
		        $size += $file->getSize();
		    } else {
		    	echo $file->getPathname() . "<br/>\n";
		    }
		}
		echo "<br/>\nTotal file size: ", $size, " bytes<br/>\n";
        $results = null;
    }
    function cmd ($cmd) {
        $ret = trim(shell_exec($cmd));
        return $ret;
    }
    function get ($host, $user, $pass, $share, $srcfile, $destfile) {
        $srcfile = $this->clean($srcfile, '\\',false);
        $destfile = $this->clean($destfile, '/',false);
        $fullcmd = 'smbclient //' . $host . '/' . $share;
        if ($user != '') {
            $fullcmd .= ' -U ' . $user;
            if ($pass != '') {
                $fullcmd .= '%' . $pass;
            }
        }
        $fullcmd .= " -c 'get \"$srcfile\" \"$destfile\";'";
        $ret = $fullcmd . " at " . time() . "<br />\n";
        $shell_ret = '';
        $shell_ret = shell_exec($fullcmd);
        if ($shell_ret == '') {
            $ret .= "File transfered.<br />\n";
        } else {
            $ret .= "$shell_ret<br />\nError<br />\n";
        }
        return $ret;
    }
    function put ($host, $user, $pass, $share, $srcfile, $destfile) {
        $srcfile = $this->clean($srcfile, '\\',false);
        $destfile = $this->clean($destfile, '/',false);
        $fullcmd = 'smbclient //' . $host . '/' . $share;
        if ($user != '') {
            $fullcmd .= ' -U ' . $user;
            if ($pass != '') {
                $fullcmd .= '%' . $pass;
            }
        }
        $fullcmd .= " -c 'put \"$srcfile\" \"$destfile\";'";
        $ret = $fullcmd . " at " . time() . "<br />\n";
        $shell_ret = '';
        $shell_ret = shell_exec($fullcmd);
        if ($shell_ret == '') {
            $ret .= "File transfered.<br />\n";
        } else {
            $ret .= "$shell_ret<br />\nError<br />\n";
        }
        return $ret;
    }
    function clean($path, $use_slash, $remove_preceding_slash) {
        $path = trim($path);
        if ($use_slash == '') {
            $use_slash = '/';
        }
        if (empty($path)) {
            $path = JPATH_ROOT;
        } else {
            // Remove double slashes and backslahses and convert all slashes and backslashes to DS
            $outpath = ereg_replace("\.[\.]+", "", $path);
            if ($remove_preceding_slash) {
                $outpath = ereg_replace("^[\/]+", "", $outpath);
            }
            $outpath = preg_replace('#[/\\\\]+#', $use_slash, $outpath);
            $outpath = ereg_replace("^[A-Za-z][:\|][\/]?", "", $outpath);
        }
        return $outpath;
    }

######################################
}
?>